package com.cl.cloud.auth.handler;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.AbstractOAuth2SecurityExceptionHandler;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 自定义资源认证异常处理器
 * @author: liuzijian
 * @time: 2022-05-12 11:01
 */
public class ResourceAccessDeniedHandler extends AbstractOAuth2SecurityExceptionHandler implements AuthenticationEntryPoint {

    public ResourceAccessDeniedHandler(WebResponseExceptionTranslator<OAuth2Exception> exceptionTranslator) {
        super.setExceptionTranslator(exceptionTranslator);
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        //这个方法底层会调用 exceptionTranslator里面的translate方法
        this.doHandle(request, response, authException);
    }
}
